﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Policy;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows;
using Yxinmt.Controls;
using Yxinmt.Interface;
using Yxinmt.Utility;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace Yxinmt.Windows.Meeting
{
    public class FileReaderTab : BaseWindow, IWindow
    {
        Controls.InkCanvas _InkCanvas { get; set; }
        IControl _ScrollViewer1 { get; set; }

        DocumentViewing _DocumentViewing { get; set; }

        Entity.FileInfo _File { get; set; }

        public FileReaderTab(System.Windows.Window form, IWindow parentForm, System.Windows.Controls.Grid pagePanel, Dictionary<string,object> parameters)
        {
            Window = form;
            ParentWindow = parentForm;
            _PagePanel = pagePanel;
            _Parameters = parameters;
            _DocumentViewing = ParentWindow as DocumentViewing;
            _File = parameters.GetValue<Entity.FileInfo>("File");
            InitSet();

            initView();

            this.SetWindow();

            initLoad();
        }

        public byte[] GetCaptureScreenImage()
        {
            var r = SystemParameters.PrimaryScreenWidth / 1920;
            var w = Grid.ActualWidth*r;
            var h = Grid.ActualHeight*r;

            var window = System.Windows.Application.Current.MainWindow;
            DpiScale dpi = VisualTreeHelper.GetDpi(window);

            w = w * dpi.DpiScaleX;
            h = h * dpi.DpiScaleY;

            System.Windows.Point p = Grid.PointToScreen(new System.Windows.Point());

            return Utility.Common.CaptureScreen(Convert.ToInt32(p.X), Convert.ToInt32(p.Y), Convert.ToInt32(w), Convert.ToInt32(h));
        }

        Controls.WindowsFormsHost _WindowsFormsHost1 { get; set; }
        Controls.WebView2 _WebView1 { get; set; }
        Controls.WebView2 _WebView3 { get; set; }
        Controls.Image _img_loading { get; set; }

        void initView()
        {
            var webview3 = Utility.Common.GetProperty(_View, "WebView3");
            webview3["Url"] = AppDomain.CurrentDomain.BaseDirectory + "html\\loading.html";
        }

        void initLoad()
        {
            _ScrollViewer1 = GetControl("ScrollViewer1");
            _WindowsFormsHost1 = (Controls.WindowsFormsHost)GetControl("WindowsFormsHost1");
            _WebView1 = (Controls.WebView2)GetControl("WebView1");
            _WebView3 = (Controls.WebView2)GetControl("WebView3");
            _img_loading = (Controls.Image)GetControl("img_loading");
            _InkCanvas = (Controls.InkCanvas)GetControl("InkCanvas1");

            _FileReader2 = new FileReader2(this, _WebView1, _WindowsFormsHost1, _DocumentViewing.ClientConfig.ServiceUrl);

            _WebView3.ReceiveMessage = (msg) =>
            {
                if (msg.GetStringValue("type") == "load") setFileLoad();
            };
        }

        FileReader2 _FileReader2 { get; set; }

        public void setFileLoad()
        {
            Task.Run(() =>
            {
                Thread.Sleep(1000);

                try
                {
                    _FileReader2.OpenFile(_DocumentViewing.MeetingId, _File, (msg) =>
                    {
                        _WebView3.SetVisible(false);
                        if (!string.IsNullOrEmpty(msg))
                        {
                            Alert(msg);
                        }
                    }, (message) =>
                    {
                        Dictionary<string, object> data = new Dictionary<string, object>();
                        data["message"] = message;
                        _WebView3.Send("progress", data);
                    });
                }
                catch (Exception ex)
                {
                    _FileReader2.Close();
                    _WebView3.SetVisible(false);
                    Alert(Name, "DataLoad", ex);
                }
            });
        }

        public override void ShowLoad()
        {
            base.ShowLoad();

            _FileReader2.Activate();
        }

        public override void CloseWindow()
        {
            base.CloseWindow();

            TaskRun(Name, true, () => {
                Thread.Sleep(100);
                Window.Dispatcher.Invoke(() =>
                {
                    _FileReader2.Close();
                });
            });
        }
    }
}

